//
//  ZQStatusToolbar.m
//  柳州微博
//
//  Created by Apple_Lzzy45 on 16/10/26.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "ZQStatusToolbar.h"
#import "ZQStatueCell.h"
#import "ZQStatus.h"
@interface ZQStatusToolbar()
/**转发按钮*/
@property(nonatomic,strong)UIButton *retweetBtn;
/**评论按钮*/
@property(nonatomic,strong)UIButton *commentBtn;
/**点赞按钮*/
@property(nonatomic,strong)UIButton *attributeBtn;
/**数组按钮*/
@property(nonatomic,strong)NSMutableArray *btns;
/**数组按钮*/
@property(nonatomic,strong)NSMutableArray *dividers;


@end
@implementation ZQStatusToolbar
-(NSMutableArray *)dividers
{
    if (_dividers==nil) {
        _dividers=[[NSMutableArray alloc]init];
    }
    return _dividers;
}
-(NSMutableArray *)btns
{
    if (_btns==nil) {
        _btns=[[NSMutableArray alloc]init];
    }
    return _btns;
}
+(instancetype)toolbar
{
    return [[self alloc]init];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        //创建三个按钮
        /*转发*/
        self.retweetBtn=[self setupBtnWithTitle:@"转发" icon:@"timeline_icon_retweet"];
        /*评论*/
         self.commentBtn=[self setupBtnWithTitle:@"评论" icon:@"timeline_icon_comment"];
        /*点赞*/
        self.attributeBtn=[self setupBtnWithTitle:@"点赞" icon:@"timeline_icon_unlike"];
        
        [self setupDivider];
         [self setupDivider];
        
    }
    return self;
}
//创建分割线
-(void)setupDivider
{
    //创建两条分割线
    UIImageView *divider=[[UIImageView alloc]init];
    divider.image=[UIImage imageNamed:@"timeline_card_bottom_line"];
    [self addSubview:divider];
    [self.dividers addObject:divider];
    }
/**
 *  创建按钮
 *
 *  @param title 按钮名称
 *  @param icon  按钮图片
 *
 *  @return btn
 */
-(UIButton *)setupBtnWithTitle:(NSString *)title icon:(NSString *)icon
{
    UIButton *btn=[[UIButton alloc]init];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.titleLabel.font=ZQCellToolbarZiTi;
    [btn setBackgroundImage:[UIImage imageNamed:@"timeline_card_bottom_background"] forState: UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [self.btns addObject:btn];
    
    [self addSubview:btn];
    
    return btn;
    
}

//子控件布局
-(void)layoutSubviews
{
    [super layoutSubviews];
    //设置按钮frame
    NSUInteger count=self.btns.count;
    CGFloat btnW=self.width/count;
    CGFloat btnH=self.height;
    
    for (int i=0; i<count; i++) {
        UIButton *btn=self.btns[i];
        btn.x=i*btnW;
        btn.y=0;
        btn.width=btnW;
        btn.height=btnH;
    }
    //设置分割线的frame
    
    NSUInteger dividerCount =self.dividers.count;
    
    for (int i =0; i<dividerCount; i++) {
        UIImageView *divider =self.dividers[i];
        
        divider.x =(i+1) *btnW;
        divider.y =0;
        divider.width =1;
        divider.height =self.height;
    }
}
/**
 *  微博数据设置
 */
-(void)setStatus:(ZQStatus *)status
{
    _status =status;
    
    //转发数
    [self setupBtnCount:status.reposts_count title:@"转发" btn:self.retweetBtn];
  
    //评论数
    [self setupBtnCount:status.comments_count title:@"评论" btn:self.commentBtn];
    

    //点赞数
   [self setupBtnCount:status.attitudes_count title:@"点赞" btn:self.attributeBtn];
  //  [self setupBtnCount:123456 title:@"点赞" btn:self.attributeBtn];
   }


-(void)setupBtnCount:(int)count title:(NSString *)title btn:(UIButton *)btn
{

    if (count) {//非零
        if (count>=10000) {
            //120000 = 12.0
            CGFloat wan =count/10000.0;
            
            //123456/1000 =120  %10  =0
            int yu =(count/1000)%10;
            if (yu) {
                title =[NSString stringWithFormat:@"%.1f万",wan];
            }else
            {title =[NSString stringWithFormat:@"%.0f万",wan];}
            
            
            
                   }else  //1~9999
        {
            title =[NSString stringWithFormat:@"%d",count];
        }
    }
   
    [btn setTitle:title forState:UIControlStateNormal];
}
@end
